package org.jetlinks.pro.device.measurements;

import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.Headers;
import org.jetlinks.pro.PropertyConstants;
import org.jetlinks.pro.tenant.utils.TenantUtils;
import org.jetlinks.pro.utils.ConverterUtils;
import org.springframework.util.StringUtils;

import java.util.*;

public class DeviceMeasurementUtils {

    public final static Set<String> disableTags = new HashSet<>(
        Arrays.asList(
            StringUtils.tokenizeToStringArray(System.getProperty("jetlinks.device-message.meter.tags.disabled", ""), ",")
        )
    );

    public static String[] getDeviceMessageTags(DeviceMessage message) {
        Map<String, Object> tagMap = new LinkedHashMap<>(TenantUtils.createBindingKey(message.getHeaders()));
        String productId = message
            .getHeader(Headers.productId)
            .map(String::valueOf)
            .orElse("unknown");
        tagMap.put(PropertyConstants.productId.getKey(), productId);
        disableTags.forEach(tagMap::remove);

        return ConverterUtils.convertMapToTags(tagMap);

    }

}
